﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntitiesLayer;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using DAL.Repositories;

namespace DAL.Repositorios
{
    public class RolProcessRepo : IRolProcessRepo
    {
        public RolSimple GetById(int id)
        {
            RolSimple rol = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdRol", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaBuscarRolXId");

            foreach (DataRow dr in ds.Tables[0].Rows)
                rol = crearRol(id, Convert.ToString(dr["NombreRol"]));

            return rol;   
        }

        internal static RolSimple crearRol(int id, String pnombre){
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdRol", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "PaConsultarPermisosRol");
            List<int> lpermiso = new List<int>();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    lpermiso.Add(Convert.ToInt32(dr["IdPermiso"]));
                }
            }
            return new RolSimple(id, pnombre, lpermiso);
        }

    }
}
